package de.poketradeshop.ui.service;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;

/**
 * Not used any more
 * Encryptes and decrypts password
 *@author http://www.java2s.com/Code/Java/Security/BasicsymmetricencryptionexamplewithCTRusingDES.htm
 */

//not used in this version

public class CipherService {
	
	public byte[] encryptPassword(String password)
			throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, IllegalBlockSizeException,
			BadPaddingException, NullPointerException, IllegalArgumentException {
	

		byte[] key = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef };
		Cipher cipher = Cipher.getInstance("DES");
		Key desKey = new SecretKeySpec(key, "DES");
		cipher.init(Cipher.ENCRYPT_MODE, desKey);
		byte[] encryptedPassword = cipher.doFinal(password.getBytes());
		return encryptedPassword;
	}

	
	public String decryptPassword(byte[] password)
			throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, IllegalBlockSizeException,
			BadPaddingException, NullPointerException, IllegalArgumentException {
		
		String decryptPassword= null;
		byte[] key = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef };
		Cipher cipher = Cipher.getInstance("DES");
		Key desKey = new SecretKeySpec(key, "DES");
		cipher.init(Cipher.DECRYPT_MODE, desKey);
		byte[] textDecrypted = cipher.doFinal(password);
		decryptPassword = new String(textDecrypted);
		return decryptPassword;
	}
}
